/*函数节流*/
function throttle(fn, gapTime) {
  // var enterTime = 0;//触发的时间
  // var gapTime = interval || 300;  //间隔时间，如果interval不传，则默认300ms
  // return function () {
  //   var context = this;
  //   var backTime = new Date();    //第一次函数return即触发的时间
  //   if (backTime - enterTime > gapTime) {
  //     fn.call(context, arguments);
  //     enterTime = backTime;       //赋值给第一次触发的时间，这样就保存了第二次触发的时间
  //   }
  // };
  if (gapTime == null || gapTime == undefined) {
    gapTime = 1500
}
let _lastTime = null
// 返回新的函数
return function () {
    let _nowTime = + new Date()
    if (_nowTime - _lastTime > gapTime || !_lastTime) {
        fn.apply(this, arguments)   //将this和参数传给原函数
        _lastTime = _nowTime
    }
}
}


/*函数防抖*/
function debounce(fn, interval) {
  var timer;
  var gapTime = interval || 1000; //间隔时间，如果interval不传，则默认1000ms
  return function () {
    clearTimeout(timer);
    var context = this;
    var args = arguments;         //保存此处的arguments，因为setTimeout是全局的，arguments不是防抖函数需要的。
    timer = setTimeout(function () {
      fn.call(context, args);
    }, gapTime);
  };
}
// 多函数导出用export default，当然了函数也是对象，用{}

// 验证中文
let chinese = /^[a-zA-Z\u4e00-\u9fa5]+$/;
// 验证手机号码
let phone =  /^1[3456789]\d{9}$/;
// 验证身份证
let card = /(^\d{15}$)|(^\d{17}(\d|X)$)/

// 倒计时
 function countTime(time,isNum,nowDate) {
	 if(nowDate){
		 var now = nowDate;
	 }else{
		var date = new Date();
		var now = date.getTime();               
	 }
	var endDate = new Date(time);//设置截止时间
	var end = endDate.getTime();
	var leftTime = end - now; //时间差 
	var d, h, m, s, ms;
	if(leftTime >= 0) {
		d = Math.floor(leftTime / 1000 / 60 / 60 / 24)
		h = Math.floor(leftTime / 1000 / 60 / 60 % 24)+d*24;
		m = Math.floor(leftTime / 1000 / 60 % 60);
		s = Math.floor(leftTime / 1000 % 60);
		if(ms < 100) {
			ms = "0" + ms;
		}
		if(s < 10) {
			s = "0" + s;
		}
		if(m < 10) {
			m = "0" + m;
		}
		if(h < 10) {
			h = "0" + h;
		}
		if(isNum){
			return h +':'+ m +":"+ s;
		}else{
			return "剩余"+h +':'+ m +":"+ s;
		}
	} else {
		// console.log('已截止')
		//将倒计时赋值到div中
		return "已结束"
	}
	//递归每秒调用countTime方法，显示动态时间效果
   // return leftTime
}

// 倒计时
 function countNewTime(leftTime) {
	var d, h, m, s, ms;
	if(leftTime >= 0) {
		d = Math.floor(leftTime / 60 / 60 / 24)
		h = Math.floor(leftTime  / 60 / 60 % 24)+d*24;
		m = Math.floor(leftTime  / 60 % 60);
		s = Math.floor(leftTime  % 60);
		if(ms < 100) {
			ms = "0" + ms;
		}
		if(s < 10) {
			s = "0" + s;
		}
		if(m < 10) {
			m = "0" + m;
		}
		if(h < 10) {
			h = "0" + h;
		}
		
		return h +':'+ m +":"+ s;
		
	} else {
		// console.log('已截止')
		//将倒计时赋值到div中
		return "已结束"
	}
	//递归每秒调用countTime方法，显示动态时间效果
   // return leftTime
}
// 转换时间格式
function formateDate(datetime) {
      // let  = "2019-11-06T16:00:00.000Z"
        function addDateZero(num) {
            return (num < 10 ? "0" + num : num);
        }
        let d = new Date(datetime);
        let formatdatetime = d.getFullYear() + '-' + addDateZero(d.getMonth() + 1) + '-' + addDateZero(d.getDate())+" "+addDateZero(d.getHours())+":"+addDateZero(d.getMinutes());
        return formatdatetime;
}

let region = [
	{
	"letter": "C",
	"data": ["潮州市"]
	},
	{
	"letter": "D",
	"data": ["东莞市"]
	},
	{
	"letter": "F",
	"data": ["佛山市"]
	},
	{
	"letter": "G",
	"data": ["广州市"]
	},
	{
	"letter": "H",
	"data": [
		"河源市",
		"惠州市",
		]
	},
	{
	"letter": "J",
	"data": [
		"江门市",
		"揭阳市",
		]
	},
	{
	"letter": "M",
	"data": [
		"茂名市",
		"梅州市",
		]
	},
	{
	"letter": "Q",
	"data": ["清远市"]
	},
	{
	"letter": "S",
	"data": [
		"韶关市",
		"汕头市",
		"汕尾市",
		"深圳市",
		]
	},
	{
	"letter": "Y",
	"data": [
		"云浮市",
		"阳江市",
	]
	},
	{
	"letter": "Z",
	"data": [
		"珠海市",
		"湛江市",
		"肇庆市",
		"中山市",
	]
	}
];
function getLength(val) {  
        var str = new String(val);  
        var bytesCount = 0;  
        for (var i = 0 ,n = str.length; i < n; i++) {  
            var c = str.charCodeAt(i);  
            if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {  
                bytesCount += 1;  
            } else {  
                bytesCount += 2;  
            }  
        }  
        return bytesCount;  
    }  
	
	// 时分秒转秒
function turnSecond(h,m,s) {
	return Number(h*3600) + Number(m*60) + Number(s);
}	

// 获取明天日期
function turnDate(date) {
	var day3 = new Date(date);
	day3.setTime(day3.getTime()+24*60*60*1000)
	let month = day3.getMonth()+1;
	if(month<10){
		month ='0'+month
	}
	let day = day3.getDate();
	if(day<10){
		day = '0'+day
	}
	return day3.getFullYear()+"-" + month + "-" + day
}	

module.exports = {
  throttle,
  debounce,
  chinese,
  phone,
  card,
  countTime,
  region,//地区
  getLength,
  countNewTime,
  formateDate,
  turnSecond,
  turnDate
};